import { deleteMenuApi } from "@/api/menu/menu"
import { Result } from "@/http/request"
import { EditType } from "@/type/BaseEnum"
import { addMenuApi,editMenuApi } from '@/api/menu/menu'
import { ref } from 'vue'
import { AddMenuModel } from '@/api/menu/menuModel'
import userInstance from '@/hooks/userInstance'

export default function tableCurd(getList) {
    // 全局属性
    const {global} = userInstance()

    // 新增弹框ref属性
    const addRef = ref<{ show111:(type:string, row?: AddMenuModel) => void }>()
    // 新增
    const addBtn = () => {
        addRef.value?.show111(EditType.ADD)
    }
    // 编辑
    const editBtn = (row) => {
        addRef.value?.show111(EditType.EDIT, row)
    }
    // 删除
    const deleteBtn = async (id: number) => {
        let params = {
            id: id
        }
        let confirm = await global.$myConfirm('确定删除该数据吗?')
        if(confirm) {
            let res = await deleteMenuApi(params)
                if(res && res.code === 200) {
                global.$message({
                    message: res.msg,
                    type: 'success'
                })
                getList()
            }
        }
    }
    // 新增/编辑保存
    const save = async (params: AddMenuModel) => {
        console.log('父组件接收到参数===', params)
        let res : Result
        if(params.editType === EditType.ADD) {
            res = await addMenuApi(params)

        } else {
            res = await editMenuApi(params)
        }
        if(res && res.code === 200) {
            // 刷新列表
            getList()
            // 信息提示
            global.$message({
                message: res.msg,
                type: 'success'
            })
        }
    }
    return {
        addRef,
        addBtn,
        editBtn,
        deleteBtn,
        save
    }
}